package com.sy.cloud.wechat.service;

import com.sy.cloud.wechat.service.AbstractWeChatClient;
import com.sy.cloud.wechat.service.AuthorizeService;
import com.sy.cloud.wechat.service.ConfigService;
import com.google.gson.Gson;
import com.sy.common.persistence.BaseDao;
import com.sy.cloud.wechat.entity.AccessToken;
import com.sy.cloud.wechat.entity.WeChatUserInfo;
import com.sy.cloud.wechat.model.WechatUser;
import com.sy.common.service.IBaseService;
import com.sy.common.service.ITestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhuwenquan on 2017-10-30.
 */
@Service
@Slf4j
public class WeChatClient extends AbstractWeChatClient {

    private ConfigService configService = new ConfigService();
    private AuthorizeService authorizeService = new AuthorizeService();


    @Resource
    ITestService testService;

    @Resource
    IBaseService baseService;



    @Override
    protected String getAccessToken() {
        log.info("getAppId"+getAppId());
        log.info("getSecret"+getSecret());
        AccessToken atmodel = new AccessToken();
        atmodel = configService.getAccessToken(getAppId(),getSecret());
        log.info("AccessToken"+atmodel.getAccessToken());
        return atmodel.getAccessToken();
    }

    @Override
    protected String getAppId() {
        return "wx48f94b9334cd2a7e";
    }

    @Override
    protected String getSecret() {
        return "d59bc9f3a830bbcbd953f5b20b3e8cfe";
    }


    public void getWechatUser(String openid,String weixinId) {
        log.info("openid"+openid);
        WeChatUserInfo wui = new WeChatUserInfo();
        String accesstoken = testService.getAccesstoken( weixinId );
        log.info("获得的accesstoken是"+ accesstoken );
        wui = authorizeService.getWeChatSubscribeUserInfo(  accesstoken , openid );
        log.info("微信用户的信息获得结束2"+new Gson().toJson(wui));


        StringBuilder hql = new StringBuilder();
        hql.append("From WechatUser where open_id = '");
        hql.append( wui.getOpenid());
        hql.append("'");

        WechatUser usermodel = new WechatUser();

        usermodel = baseService.singleQueryFromMaster(hql.toString(), WechatUser.class );

        if( usermodel == null ){
            usermodel = new WechatUser();
        }

        usermodel.setCity(wui.getCity());
        usermodel.setCountry(wui.getCountry());
        usermodel.setProvince(wui.getProvince());
        usermodel.setOpen_id(wui.getOpenid());
        usermodel.setWechat_name(wui.getNickname());
        usermodel.setSex(wui.getSex());
        String imgurl = wui.getHeadimgurl();
        imgurl = imgurl.substring( 0, imgurl.length()-1 );
        usermodel.setHeadimgurl(imgurl);
        usermodel.setSubscribe(wui.getSubscribe());
        usermodel.setSubscribe_time(wui.getSubscribe_time());
        usermodel.setLanguange(wui.getLanguage());

        testService.addModel(usermodel);

    }

//    public String echoTxtStr(ClickEventMessage xs){
//        Date now = new Date();
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
//        TextMessage tm = new TextMessage();
//        String in = "<a>登录路口，点击后失效</a>有效期至"+dateFormat;
//        tm.setContent(in);
//        tm.setCreateTime(xs.getCreateTime());
//        tm.setMsgType(WeChatMessage.MsgType.text);
//        tm.setOpenId(getAppId());
//        tm.setWeixinId( xs.getWeixinId() );
//        return tm.toReturnXML();
//
//    }
}
